package com.mathassistant.client.view;

import java.util.ArrayList;
import java.util.Random;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.VerticalPanelDropController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.exercise.ExerciseStepHandler;
import com.mathassistant.client.helper.MyHTML;
import com.mathassistant.client.presenter.ExerciseStepPresenter.Display;

public class ExerciseStepView extends Composite implements Display {

	private static ExerciseStepViewUiBinder uiBinder = GWT
			.create(ExerciseStepViewUiBinder.class);
	@UiField
	HTMLPanel panelListAnswer;
	@UiField
	DecoratorPanel anwserPanel;
	@UiField
	Button checkButton;
	@UiField
	Image image;
	@UiField
	Button nextButton;
	@UiField
	HTML questionHTML;
	@UiField
	Label exercisePerTotal;

	private static final String CSS_DEMO_INSERT_PANEL_EXAMPLE = "demo-InsertPanelExample";
	private static final String CSS_DEMO_INSERT_PANEL_EXAMPLE_WIDGET = "demo-InsertPanelExample-widget";

	private static final int SPACING = 0;

	String corrImageUrl = "./Images/face-smiley.gif";
	String incorrImageUrl = "./Images/face-sad.gif";

	VerticalPanel vPanel;
	ArrayList<MyHTML> widgetAnswers;

	interface ExerciseStepViewUiBinder extends
			UiBinder<Widget, ExerciseStepView> {
	}

	public ExerciseStepView() {
		initWidget(uiBinder.createAndBindUi(this));
		image.setVisible(false);
		nextButton.setVisible(false);
	}

	@Override
	public void setData(ArrayList<String> listAnswer) {
		if(widgetAnswers != null)
			widgetAnswers.clear();
		if(vPanel != null)
			vPanel.clear();
		
		widgetAnswers = new ArrayList<MyHTML>();
		vPanel = new VerticalPanelWithSpacer();
		
		AbsolutePanel boundaryPanel = new AbsolutePanel();
		boundaryPanel.setSize("100%", "100%");

		ExerciseStepHandler handler = new ExerciseStepHandler();

		PickupDragController dragControler = new PickupDragController(
				boundaryPanel, false);
		dragControler.setBehaviorMultipleSelection(false);
		dragControler.addDragHandler(handler);

		// vPanel.setStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_CONTAINER);
		vPanel.setSpacing(SPACING);
		vPanel.setSize("95%", "95%");

		VerticalPanelDropController dropControler = new VerticalPanelDropController(
				vPanel);

		dragControler.registerDropController(dropControler);

		for (int i = 0; i < listAnswer.size(); i++) {

			MyHTML wg = new MyHTML(listAnswer.get(i));
			wg.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE_WIDGET);
			wg.setWidth("100%");
			wg.setHeight(5 + "em");
			wg.setAnsNumber(i);
			widgetAnswers.add(wg);
			dragControler.makeDraggable(wg);

		}
		embroilAnswersWidgetToVPanel();
		boundaryPanel.add(vPanel);
		panelListAnswer.addStyleName(CSS_DEMO_INSERT_PANEL_EXAMPLE);
		panelListAnswer.add(boundaryPanel);
		
		image.setVisible(false);
		nextButton.setVisible(false);
		checkButton.setVisible(true);
	}

	// test in 10h58PM 23.5.2011, run ok
	void embroilAnswersWidgetToVPanel() {
		int max = widgetAnswers.size();
		ArrayList<Integer> arrRandom = new ArrayList<Integer>();

		Random random = new Random();

		int k = 0;
		while (k < max) {
			int r = random.nextInt(max);
			boolean had = false;
			for (Integer integer : arrRandom) {
				if (integer == r)
					had = true;
			}
			if (had == false) {
				arrRandom.add(r);
				k++;
			}
		}

		for (int i = 0; i < arrRandom.size(); i++) {

			vPanel.add(widgetAnswers.get(arrRandom.get(i)));
		}
	}

	@Override
	public boolean getCorrectAns() {
		// Chua hieu tai sap vPanel.getWidgetCount() > so voi so widget that su
		// co trong do (lon hon 1 don vi)
		for (int i = 0; i < widgetAnswers.size(); i++) {
			if (((MyHTML) vPanel.getWidget(i)).getAnsNumber() != i)
				return false;
		}
		return true;
	}

	@Override
	public HasClickHandlers getCheckButton() {
		// TODO Auto-generated method stub
		return checkButton;
	}

	@Override
	public HasClickHandlers getNextButton() {
		// TODO Auto-generated method stub
		return nextButton;
	}

	@Override
	public void toCorrectView() {
		// TODO Auto-generated method stub
		checkButton.setVisible(false);
		image.setVisible(true);
		image.setUrl(corrImageUrl);
		nextButton.setVisible(true);
	}

	@Override
	public void toWrongView() {
		// TODO Auto-generated method stub
		image.setVisible(true);
		image.setUrl(incorrImageUrl);
	}

	@Override
	public void toEmptyView() {
		// TODO Auto-generated method stub
		anwserPanel.setVisible(false);
		questionHTML.setHTML("Chưa có bài tập nào! ^^");
	}

	@Override
	public void toFinishView() {
		// TODO Auto-generated method stub
		anwserPanel.setVisible(false);
		questionHTML.setHTML("Hoàn thành chủ đề");
	}

	@Override
	public void setExPerTotal(String indexPerTotal) {
		// TODO Auto-generated method stub
		exercisePerTotal.setText(indexPerTotal);
	}

	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}
}
